<?php
/*******************************************************************
 * [JishiGou] (C)2005 - 2099 Cenwor Inc.
 *
 * This is NOT a freeware, use is subject to license terms
 *
 * @Filename journals.logic.php $
 *
 * @Author http://www.jishigou.net $
 *
 * @Date 2013-04-26 08:42:17 1484620860 97685394 19993 $
 *******************************************************************/
if (! defined ( 'IN_JISHIGOU' )) {
	exit ( 'invalid request' );
}
class JournalsLogic {
	function JournalsLogic() {
	}
	function is_exists($id) {
		$count = DB::result_first ( "SELECT COUNT(*) FROM " . DB::table ( 'journals' ) . " WHERE id='{$id}'" );
		return $count;
	}
	function get_journals_info($id) {
		$journals_info = DB::fetch_first ( "SELECT * FROM " . DB::table ( 'journals' ) . " WHERE id='{$id}'" );
		return $journals_info;
	}
	function createJournals($post, $item = '', $item_id = 0, $verify = 1) {
		$qua_arr = array ();
		if ($post ['qua'] == 'qua') {
			if ($post ['fans']) {
				$qua_arr ['fans_num'] = $post ['fans_num'];
			}
			if ($post ['same_city']) {
				$qua_arr ['same_city'] = 1;
			}
			if ($post ['inqun']) {
				$qua_arr ['inqun'] = $post ['inqun'];
			}
		}
		$qualification = serialize ( $qua_arr );
		
		$need_app_info = $post ['info'] ? serialize ( $post ['info'] ) : serialize ( array () );
		
		$postman = MEMBER_ID;
		if ($post ['money_r'] == 'money') {
			$money = $post ['money'];
		} else {
			$money = 0;
		}
		$time = TIMESTAMP;
		$fromt = strtotime ( $post ['fromt'] . " " . $post ['hour_select_from'] . ":" . $post ['min_select_from'] );
		$tot = strtotime ( $post ['tot'] . " " . $post ['hour_select_to'] . ":" . $post ['min_select_to'] );
		$post ['content1'] = str_replace ( '\"', '"', $post ['content1'] );
		
		if ($post [act]) {
			$sql = "update " . TABLE_PREFIX . "journals
	    			set
	    			    type_id = '$post[type]',
	    			    title = '$post[name]',
	    			    content = '$post[content1]',
	    			    image = '$post[hid_pic]',
	    			    province_id = '$post[province]',
	    			    area_id = '$post[area]',
	    			    city_id = '$post[city]',
	    			    address = '$post[address]',
	    			    lasttime = $time,
	    			    qualification = '$qualification',
	    			    postip = '" . client_ip () . "',
	    			    need_app_info = '$need_app_info'
	    			where id  = '$post[id]'";
			DB::query ( $sql );
			$values = array (
					'id' => $post [id] 
			);
			return $values;
		}
		
		$sql = "insert into " . TABLE_PREFIX . "journals (
	    			type_id,title,content,
	    			image,province_id,area_id,city_id,address,
	    			postman,posttime,lasttime,qualification,need_app_info,verify,postip,item,item_id
	    ) values (
	    			'$post[type]','$post[name]','$post[content1]',
	    			'$post[hid_pic]','$post[province]','$post[area]','$post[city]','$post[address]',
	    			'$postman',$time,$time,'$qualification','$need_app_info',$verify,'" . client_ip () . "','$item','$item_id'
	    )";
		DB::query ( $sql );
		$id = DB::insert_id ();
		
		if ($item == 'qun' && $item_id) {
			DB::query ( "insert into `" . TABLE_PREFIX . "qun_journals` (`qid`,`eid`) values ('$item_id','$id')" );
		}
		
		if ($verify == 0) {
			if ($notice_to_admin = $GLOBALS ['_J'] ['config'] ['notice_to_admin']) {
				$pm_post = array (
						'message' => MEMBER_NICKNAME . "发布了一个游记进入待审核状态，<a href='nanbo.php?mod=journals&code=verify' target='_blank'>点击</a>进入审核。",
						'to_user' => str_replace ( '|', ',', $notice_to_admin ) 
				);
				$admin_info = DB::fetch_first ( 'select `uid`,`username`,`nickname` from `' . TABLE_PREFIX . 'members` where `uid` = 1' );
				load::logic ( 'pm' );
				$PmLogic = new PmLogic ();
				$PmLogic->pmSend ( $pm_post, $admin_info ['uid'], $admin_info ['username'], $admin_info ['nickname'] );
			}
		}
		return $id;
	}
	function delJournals($id, $admin = 0) {
		$journals = DB::fetch_first ( "select * from " . TABLE_PREFIX . "journals where id = '$id' " );
		if (! $journals) {
			return 2;
		}
		$image = $journals ['image'];
		if ($image) {
			$type = trim ( strtolower ( end ( explode ( ".", $image ) ) ) );
			$name = explode ( "_", $image );
			$image_s = $name [0] . "_s." . $type;
			
			unlink ( $image );
			unlink ( $image_s );
		}
		DB::query ( "delete from " . TABLE_PREFIX . "journals where id = '$id' " );
		return;
	}
	/**
	 * 游记的顶和踩
	 * 成功返回true 
	 * 失败返回错误提示
	 * @param int $id	游记id
	 * @param string $eva	up:顶,down:踩
	 * @return string|boolean	
	 */
	function evaluate($id,$eva){
		$jEva=DB::fetch_first("select uid from ".TABLE_PREFIX."journals_evaluate where journals_id={$id} and uid=".MEMBER_ID);
		if($jEva!=false){
			return '你已经对该游记评价过了,不能再评价了!';
		}
		$evaluates=evaluate();
		if(!in_array($eva,$evaluates)){
			return '你这个是什么操作?吓死我了!';
		}
		DB::query('update '.TABLE_PREFIX."journals set {$eva}={$eva}+1");
		DB::query("insert into ".TABLE_PREFIX."journals_evaluate(journals_id,uid,evaluate)values({$id},".MEMBER_ID.",'".array_search($eva,$evaluates)."')");
		return true;
	}
	function getJournalsInfo($param) {
		if (! empty ( $param ['where'] )) {
			$where_sql .= " {$param['where']} ";
		}
		
		$order_sql = " ";
		if (! empty ( $param ['order'] )) {
			$order_sql = " {$param['order']} ";
		}
		
		$limit_sql = " ";
		if (! empty ( $param ['limit'] )) {
			$limit_sql = " {$param['limit']} ";
		}
		
		$journals_list = array ();
		$count_sql = "select count(*) from " . TABLE_PREFIX . "journals a where $where_sql ";
		$count = DB::result_first ( $count_sql );
		
		if ($count) {
			if ($param ['page']) {
				$_config = array (
						'return' => 'array' 
				);
				$page_arr = page ( $count, $param ['perpage'], $param ['page_url'], $_config );
				$limit_sql = $page_arr ['limit'];
			}
			
			$sql = "select a.id,a.title,a.create_time,a.need_app_info,a.item,a.item_id,
	    			   a.content,a.image,fp.name as province,fa.name as area,fc.name as city,
	    			   a.address,a.verify,a.click,a.is_top,a.up,a.down,
	    			   a.postman,a.posttime,a.item,a.item_id,m.nickname,m.username,m.uid,
					   eval.evaluate
		    		from " . TABLE_PREFIX . "journals a
		    		left join " . TABLE_PREFIX . "common_district fp on fp.id = a.province_id
		    		left join " . TABLE_PREFIX . "common_district fa on fa.id = a.area_id
					left join " . TABLE_PREFIX . "common_district fc on fc.id = a.city_id
					left join " . TABLE_PREFIX . "members m on m.uid = a.postman
					left join " . TABLE_PREFIX . "journals_evaluate eval on a.id=eval.journals_id
					WHERE {$where_sql}
					{$order_sql}
					{$limit_sql} ";
			
			$query = DB::query ( $sql );
			while ( $rsdb = DB::fetch ( $query ) ) {
				/*
				 * if($rsdb['app'] == 1){ $rsdb['app'] = "已报名"; $rsdb['app_time'] = date("Y年m月d日 H:i",$rsdb['app_time']); }else{ $rsdb['app'] = "未报名"; } if($rsdb['tot'] <= TIMESTAMP){ $rsdb['journals_type'] = "游记已截止"; }else{ $rsdb['journals_type'] = "游记进行中"; $app = "style='display:none'"; } $rsdb['fromt_day'] = $this->_getDay($rsdb['fromt']); $rsdb['fromt'] = date("Y年m月d日 H:i",$rsdb['fromt']); $rsdb['tot_day'] = $this->_getDay($rsdb['tot']); $rsdb['tot'] = date("Y年m月d日 H:i",$rsdb['tot']);
				 */
				$rsdb ['address'] = $rsdb ['province'] . $rsdb ['city'] . $rsdb ['area'] . $rsdb ['address'];
				if (! $rsdb [image]) {
					$rsdb [image] = "images/kuang.png";
				}
				$journals_list [$rsdb ['id']] = $rsdb;
			}
			return array (
					'count' => $count,
					'journals_list' => $journals_list,
					'page' => $page_arr 
			);
		}
		return false;
	}
	function getJournalss($param) {
		$uid = $param ['uid'];
		
		if (! empty ( $param ['where'] )) {
			$where_sql .= " {$param['where']} ";
		}
		
		$order_sql = " ";
		if (! empty ( $param ['order'] )) {
			$order_sql = " {$param['order']} ";
		}
		
		$limit_sql = " ";
		if (! empty ( $param ['limit'] )) {
			$limit_sql = " {$param['limit']} ";
		}
		
		$journals_list = array ();
		$count_sql = "select count(*) from " . TABLE_PREFIX . "journals where verify = 1";
		$count = DB::result_first ( $count_sql );
		
		if ($count) {
			if ($param ['page']) {
				$_config = array (
						'return' => 'array' 
				);
				$page_arr = page ( $count, $param ['perpage'], $param ['page_url'], $_config );
				$limit_sql = $page_arr ['limit'];
			}
			
			$sql = "select a.id,a.title,a.image,fp.name as province,fa.name as area,fc.name as city,
					a.address,a.click,a.is_top,a.up,a.down
					from " . TABLE_PREFIX . "journals a 
					left join " . TABLE_PREFIX . "common_district fp on fp.id = a.province_id
					left join " . TABLE_PREFIX . "common_district fa on fa.id = a.area_id
					left join " . TABLE_PREFIX . "common_district fc on fc.id = a.city_id
					where a.verify = 1
					{$order_sql}
					{$limit_sql} ";
			$query = DB::query ( $sql );
			while ( $rsdb = DB::fetch ( $query ) ) {
				$rsdb ['fromt'] = date ( "Y年m月d日 H:i", $rsdb ['fromt'] );
				$rsdb ['tot'] = date ( "Y年m月d日 H:i", $rsdb ['tot'] );
				if (! $rsdb [image]) {
					$rsdb [image] = "images/kuang.png";
				}
				if ($rsdb ['app'] == 1) {
					$rsdb ['app'] = "已报名";
					$rsdb ['app_time'] = date ( "Y年m月d日 H:i", $rsdb ['app_time'] );
				} else {
					$rsdb ['app'] = "未报名";
				}
				if ($rsdb ['play'] == 1) {
					$rsdb ['play_time'] = date ( "Y年m月d日 H:i", $rsdb ['play_time'] );
				}
				if ($rsdb ['store'] == 1) {
					$rsdb ['store_time'] = date ( "Y年m月d日 H:i", $rsdb ['store_time'] );
				}
				$rsdb ['address'] = $rsdb ['province'] . $rsdb ['city'] . $rsdb ['area'] . $rsdb ['address'];
				
				$journals_list [$rsdb ['id']] = $rsdb;
			}
			
			return array (
					'count' => $count,
					'journals_list' => $journals_list,
					'page' => $page_arr 
			);
		}
		return false;
	}
	function getNewJournals($param) {
		$uid = $param ['uid'];
		$page_url = $param ['page_url'];
		$per_page_num = $param ['perpage'];
		$_config = array (
				'return' => 'array' 
		);
		$query = DB::query ( "select * from " . TABLE_PREFIX . "journals_member where fid = '$uid' " );
		while ( $rsdb = DB::fetch ( $query ) ) {
			$id_arr [$rsdb ['id']] = $rsdb ['id'];
			$count ++;
		}
		if ($id_arr) {
			$id_list = jimplode ( $id_arr );
			$where = "and a.id not in ($id_list)";
		}
		
		$sql = "select a.id,a.title,a.fromt,a.tot,a.image,a.content,a.postman,fp.name as province,fa.name as area,fc.name as city,
				m.app,m.app_time,m.play,m.play_time,m.store,m.store_time,a.address,a.click,a.is_top,a.up,a.down
				from " . TABLE_PREFIX . "journals_member m
				right join " . TABLE_PREFIX . "journals a on a.id = m.id
				left join " . TABLE_PREFIX . "common_district fp on fp.id = a.province_id
				left join " . TABLE_PREFIX . "common_district fa on fa.id = a.area_id
				left join " . TABLE_PREFIX . "common_district fc on fc.id = a.city_id
				where
				    m.fid = '$uid'
				  or
				    (a.postman = '$uid' and a.verify = 1
				    $where)
                    order by a.lasttime desc,a.posttime desc ";
		
		$postcount = DB::result_first ( "select count(*) from " . TABLE_PREFIX . "journals a where a.postman = '$uid' $where  and a.verify = 1 " );
		$count = $count + $postcount;
		$page_arr = page ( $count, $per_page_num, $page_url, $_config );
		$sql .= $page_arr ['limit'];
		if ($count) {
			$query = DB::query ( $sql );
			while ( $rsdb = DB::fetch ( $query ) ) {
				if (! $rsdb [image]) {
					$rsdb [image] = "images/kuang.png";
				}
				if ($rsdb ['app'] == 1) {
					$rsdb ['app'] = "已报名";
					$rsdb ['app_time'] = date ( "Y年m月d日 H:i", $rsdb ['app_time'] );
				} else {
					$rsdb ['app'] = "未报名";
				}
				if ($rsdb ['tot'] <= TIMESTAMP) {
					$rsdb ['journals_type'] = "游记已截止";
				} else {
					$rsdb ['journals_type'] = "游记进行中";
					$app = "style='display:none'";
				}
				$rsdb ['fromt'] = date ( "Y年m月d日 H:i", $rsdb ['fromt'] );
				$rsdb ['tot'] = date ( "Y年m月d日 H:i", $rsdb ['tot'] );
				
				if ($rsdb ['play'] == 1) {
					$rsdb ['play_time'] = date ( "Y年m月d日 H:i", $rsdb ['play_time'] );
				}
				if ($rsdb ['store'] == 1) {
					$rsdb ['store_time'] = date ( "Y年m月d日 H:i", $rsdb ['store_time'] );
				}
				$rsdb ['address'] = $rsdb ['province'] . $rsdb ['city'] . $rsdb ['area'] . $rsdb ['address'];
				$rs [$rsdb ['id']] = $rsdb;
			}
		}
		return array (
				'count' => $count,
				'journals_list' => $rs,
				'page' => $page_arr 
		);
	}
	function getAllUser($param, $type) {
		$where = $param ['where'];
		$order = $param ['order'];
		$limit_sql = $param ['limit'];
		
		if ($param ['page']) {
			$_config = array (
					'return' => 'array' 
			);
		}
		
		$sql = "select m.uid,m.nickname,m.username,m.face,m.province,m.city from " . TABLE_PREFIX . "members m 
	    		$limit_sql ";
		$query = DB::query ( $sql );
		while ( $rsdb = DB::fetch ( $query ) ) {
			$rsdb ['face'] = face_get ( $rsdb ['uid'] );
			if ($type == 'app') {
				$rsdb ['time'] = date ( "Y-m-d H:i", $rsdb ['app_time'] );
			} else {
				$rsdb ['time'] = date ( "Y-m-d H:i", $rsdb ['play_time'] );
			}
			$member [$rsdb ['uid']] = $rsdb;
		}
		$return ['member'] = $member;
		$return ['count'] = $count;
		$return ['page_arr'] = $page_arr;
		return $return;
	}
	function getHotJournals() {
		$hot_journals = array ();
		$query = DB::query ( "select * from " . TABLE_PREFIX . "journals order by lasttime desc , click desc " );
		while ( $rsdb = DB::fetch ( $query ) ) {
			if (! $rsdb ['image']) {
				$rsdb [image] = "images/kuang.png";
			} else {
				$type = trim ( strtolower ( end ( explode ( ".", $rsdb ['image'] ) ) ) );
				$name = explode ( "_", $rsdb ['image'] );
				$rsdb ['image'] = $name [0] . "_s." . $type;
			}
			$hot_journals [$rsdb ['id']] = $rsdb;
		}
		return $hot_journals;
	}
	function getDaRen() {
		$uids = array ();
		$sql = "SELECT postman
				FROM `" . TABLE_PREFIX . "journals`
				GROUP BY postman
				ORDER BY count( postman ) DESC
				LIMIT 12 ";
		$query = DB::query ( $sql );
		while ( $rsdb = DB::fetch ( $query ) ) {
			$uids [] = $rsdb ['postman'];
		}
		return $uids;
	}
	function _getDay($time) {
		$day_num = date ( "w", $time );
		switch ($day_num) {
			case 1 :
				$day = "星期一";
				break;
			case 2 :
				$day = "星期二";
				break;
			case 3 :
				$day = "星期三";
				break;
			case 4 :
				$day = "星期四";
				break;
			case 5 :
				$day = "星期五";
				break;
			case 6 :
				$day = "星期六";
				break;
			case 0 :
				$day = "星期天";
				break;
			default :
				break;
		}
		return $day;
	}
	function doStroe($id, $type) {
		$time = TIMESTAMP;
		if ($type == 'cancle') {
			DB::query ( "update " . TABLE_PREFIX . "journals_member set store = 0,store_time = '$time' where id = '$id' and fid = " . MEMBER_ID );
		} else {
			$count = DB::result_first ( "select count(*) from " . TABLE_PREFIX . "journals_member where id = '$id' and fid = " . MEMBER_ID );
			if ($count) {
				DB::query ( "update " . TABLE_PREFIX . "journals_member set store = 1,store_time = '$time' where id = '$id' and fid = " . MEMBER_ID );
			} else {
				$title = DB::result_first ( "select title from " . TABLE_PREFIX . "journals where id = '$id'" );
				DB::query ( "insert into " . TABLE_PREFIX . "journals_member (id,title,fid,store,store_time) values('$id','$title'," . MEMBER_ID . ",1,'$time')" );
			}
		}
	}
	function allowedCreate($uid = MEMBER_ID, $member) {
		if (! $member) {
			$member = DB::fetch_first ( "SELECT validate FROM " . DB::table ( 'members' ) . " WHERE uid='{$uid}'" );
		}
		$config = ConfigHandler::get ();
		if ($config ['journals_vip']) {
			if (! $member ['validate']) {
				return "非V认证用户不允许创建游记,<a href='index.php?mod=other&code=vip_intro'>点此申请V认证</a>";
			}
		}
	}
	function addClickJournals($id) {
		return DB::query ( 'update ' . TABLE_PREFIX . "journals set click=click+1 where id={$id}" );
	}
}